package com.zxy.day0624_util.utils;

import org.apache.pdfbox.io.IOUtils;
import java.io.FileInputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: lenovo
 * @Date: 2021/9/27 20:46
 * @Description:
 */
public class FileDownload {
    public FileDownload() {
    }

    public static void download(HttpServletResponse response, String source, String downloadName, int mode) throws Exception {
        String modeStr = "inline";
        if (mode == 0) {
            modeStr = "attachment";
            downloadName = new String(downloadName.getBytes("utf-8"), "iso-8859-1");
        }

        response.setHeader("Content-Disposition", modeStr + ";fileName=" + downloadName);
        FileInputStream iStream = new FileInputStream(source);
        IOUtils.copy(iStream, response.getOutputStream());
        iStream.close();
    }
}

